package com.snap.awesomeserial.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.snap.awesomeserial.R;
import com.snap.awesomeserial.adapter.StepRunningAdapter;
import com.snap.awesomeserial.base.BaseActivity;
import com.snap.awesomeserial.bean.NAStep;
import com.snap.awesomeserial.bean.TemplateInfo;
import com.snap.awesomeserial.ui.widget.AlphaFrameLayout;
import com.snap.awesomeserial.ui.widget.DialogView;
import com.snap.awesomeserial.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class RunningActivity extends BaseActivity {

    private static final String TAG = "TemplateActivity";
    @BindView(R.id.title_tv)
    TextView titleTv;

    @BindView(R.id.steps_rv)
    RecyclerView stepsRv;
    @BindView(R.id.run_fl)
    AlphaFrameLayout runFl;
    @BindView(R.id.stop_fl)
    ConstraintLayout stopFl;
    @BindView(R.id.state_tv)
    TextView stateTv;
    @BindView(R.id.temp_tv)
    TextView tempTv;
    @BindView(R.id.temp_ll)
    LinearLayout tempLl;

    private int currentCheckedId;
    private String fileName;
    private TemplateInfo templateInfo;
    private StepRunningAdapter stepAdapter;

    @Override
    protected int layoutResId() {
        return R.layout.activity_running;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            currentCheckedId = savedInstanceState.getInt("currentCheckId");
        }

        templateInfo = (TemplateInfo) getIntent().getSerializableExtra("templateInfo");
        if (templateInfo == null) {
            Logger.e("模板解析失败！");
            return;
        }
        fileName = templateInfo.getName();
        titleTv.setText(fileName);
        LinearLayoutManager manager = new LinearLayoutManager(RunningActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        stepsRv.setLayoutManager(manager);
        stepAdapter = new StepRunningAdapter(RunningActivity.this);
        stepsRv.setAdapter(stepAdapter);
        List<NAStep> naSteps = new ArrayList<>();
        for (NAStep step : templateInfo.getNAProgram()) {
            if (step.isChoose()) {
                naSteps.add(step);
            }
        }
        stepAdapter.bindDataList(naSteps);
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt("currentCheckId", currentCheckedId);
        super.onSaveInstanceState(outState);
    }

    @OnClick({R.id.pause_btn, R.id.stop_btn, R.id.start_btn})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pause_btn:
                updateViewState(false);
                // TODO: 2021/6/18 倒计时停止

                break;
            case R.id.stop_btn:
                new DialogView(this)
                        .setTitleContent("确认终止当前实验")
                        .setOnClickListener("终止", new DialogView.OnWarnListener() {
                            @Override
                            public void onWarnClick() {
                                ToastUtils.get().showText("终止当前实验成功");
                            }
                        }).show();
                break;
            case R.id.start_btn:
                updateViewState(true);
                break;
            default:
        }
    }


    private void updateViewState(boolean toRun) {
        runFl.setVisibility(toRun ? View.VISIBLE : View.GONE);
        stopFl.setVisibility(toRun ? View.GONE : View.VISIBLE);
        stateTv.setText(toRun?"运行中":"暂停中");
        tempLl.setBackground(getDrawable(toRun?R.drawable.bg_running_temp:R.drawable.bg_pause_temp));
    }
}

